{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataFrame是一个表格型的数据结构，相当于是一个二维数组，含有一组有序的列。他可以被看做是由Series组成的字典，并且共用一个索引，字典的key就是列索引，行索引和series一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   one  two\n",
       "0    1    4\n",
       "1    2    3\n",
       "2    3    2\n",
       "3    4    1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrame的创建01——包含等长度列表或Numpy数组的字典来形成DataFrame\n",
    "pd.DataFrame({'one':[1,2,3,4],'two':[4,3,2,1]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>age</th>\n      <th>name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>18</td>\n      <td>jack</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>19</td>\n      <td>mick</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>20</td>\n      <td>john</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   age  name\n",
       "0   18  jack\n",
       "1   19  mick\n",
       "2   20  john"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#定义一个数据框\n",
    "df2=pd.DataFrame(\n",
    "        data={\n",
    "        'age':[18,19,20],\n",
    "        'name':['jack','mick','john']\n",
    "                })\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>two</th>\n      <th>one</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   two  one\n",
       "0    4    1\n",
       "1    3    2\n",
       "2    2    3\n",
       "3    1    4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以通过columns参数指定顺序排列\n",
    "data = pd.DataFrame({'one':[1,2,3,4],'two':[4,3,2,1]})\n",
    "pd.DataFrame(data,columns=['two','one'])  #指定显示顺序\n",
    "#注意：columns一定要是key值中的，才能匹配的到，不然会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   one  two\n",
       "a    1    2\n",
       "b    2    1\n",
       "c    3    3"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame的创建02\n",
    "df= pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b','c']),'two':pd.Series([1,2,3],index=['b','a','c'])})\n",
    "df\n",
    "#字典的key是列索引，index是行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   1   2   3\n",
       "0   0   1   2   3\n",
       "1   4   5   6   7\n",
       "2   8   9  10  11\n",
       "3  12  13  14  15"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = pd.DataFrame(np.arange(16).reshape(4,4))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>hello</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   1   2   3      a\n",
       "0   0   1   2   3  hello\n",
       "1   4   5   6   7      5\n",
       "2   8   9  10  11      5\n",
       "3  12  13  14  15      5"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrame的增加01\n",
    "a['a']=[\"hello\",5,5,5]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>16</td>\n      <td>17</td>\n      <td>18</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22</td>\n      <td>23</td>\n      <td>24</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>28</td>\n      <td>29</td>\n      <td>30</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>35</td>\n      <td>36</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   1   2   3   4   5\n",
       "0  16  17  18  19  20  21\n",
       "1  22  23  24  25  26  27\n",
       "2  28  29  30  31  32  33\n",
       "3  34  35  36  37  38  39"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = pd.DataFrame(np.arange(16,40).reshape(4,6))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>hello</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>12</td>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>16</td>\n      <td>17</td>\n      <td>18</td>\n      <td>19</td>\n      <td>20.0</td>\n      <td>21.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22</td>\n      <td>23</td>\n      <td>24</td>\n      <td>25</td>\n      <td>26.0</td>\n      <td>27.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>28</td>\n      <td>29</td>\n      <td>30</td>\n      <td>31</td>\n      <td>32.0</td>\n      <td>33.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>35</td>\n      <td>36</td>\n      <td>37</td>\n      <td>38.0</td>\n      <td>39.0</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   1   2   3     4     5      a\n",
       "0   0   1   2   3   NaN   NaN  hello\n",
       "1   4   5   6   7   NaN   NaN      5\n",
       "2   8   9  10  11   NaN   NaN      5\n",
       "3  12  13  14  15   NaN   NaN      5\n",
       "0  16  17  18  19  20.0  21.0    NaN\n",
       "1  22  23  24  25  26.0  27.0    NaN\n",
       "2  28  29  30  31  32.0  33.0    NaN\n",
       "3  34  35  36  37  38.0  39.0    NaN"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.append(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>16</td>\n      <td>17</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22</td>\n      <td>23</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>28</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   1   3   4   5\n",
       "0  16  17  19  20  21\n",
       "1  22  23  25  26  27\n",
       "2  28  29  31  32  33\n",
       "3  34  35  37  38  39"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrame的删除01\n",
    "# del df[“列名”]  axis=0，针对行; axis=1，针对列\n",
    "del c[2]\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>16</td>\n      <td>17</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>28</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   1   3   4   5\n",
       "0  16  17  19  20  21\n",
       "2  28  29  31  32  33\n",
       "3  34  35  37  38  39"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据行索引删除：df.drop(1)删除行索引为1的数据，其中还有一个参数axis 默认为0\n",
    "c.drop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>3</th>\n      <td>66</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>66</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    a   b   c   d   e\n",
       "3  66  29  31  32  33\n",
       "4  66  35  37  38  39"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.drop([1,2])#删除多行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>16</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>28</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    0   3   4   5\n",
       "0  16  19  20  21\n",
       "1  22  25  26  27\n",
       "2  28  31  32  33\n",
       "3  34  37  38  39"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据列名删除：df.drop(\"age2\",axis=1)删除列名为age2的数据；或者使用del df['age2']\n",
    "c.drop(1,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>1</th>\n      <th>4</th>\n      <th>5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>17</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>23</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>29</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>35</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    1   4   5\n",
       "0  17  20  21\n",
       "1  23  26  27\n",
       "2  29  32  33\n",
       "3  35  38  39"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除多列\n",
    "c.drop(columns=[0,3],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>16</td>\n      <td>17</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>22</td>\n      <td>23</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>28</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    a   b   c   d   e\n",
       "0  16  17  19  20  21\n",
       "1  22  23  25  26  27\n",
       "2  28  29  31  32  33\n",
       "3  34  35  37  38  39"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrame的修改01\n",
    "# 修改列名：(获取列名：df.columns)df.columns=['age2','name2']\n",
    "c.columns=['a','b','c','d','e']\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>16</td>\n      <td>17</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>22</td>\n      <td>23</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>28</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    a   b   c   d   e\n",
       "1  16  17  19  20  21\n",
       "2  22  23  25  26  27\n",
       "3  28  29  31  32  33\n",
       "4  34  35  37  38  39"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改行索引：（获取行索引：df.index) df.index=range(1,4)\n",
    "c.index=range(1,5)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>16</td>\n      <td>999</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>22</td>\n      <td>23</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>28</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    a    b   c   d   e\n",
       "1  16  999  19  20  21\n",
       "2  22   23  25  26  27\n",
       "3  28   29  31  32  33\n",
       "4  34   35  37  38  39"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.iloc[0,1]=999\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    16\n",
       "2    22\n",
       "3    28\n",
       "4    34\n",
       "Name: a, dtype: int32"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c['a']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>66</td>\n      <td>999</td>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>66</td>\n      <td>23</td>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>66</td>\n      <td>29</td>\n      <td>31</td>\n      <td>32</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>66</td>\n      <td>35</td>\n      <td>37</td>\n      <td>38</td>\n      <td>39</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    a    b   c   d   e\n",
       "1  66  999  19  20  21\n",
       "2  66   23  25  26  27\n",
       "3  66   29  31  32  33\n",
       "4  66   35  37  38  39"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c['a']=[66,66,66,66]\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c'], dtype='object')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrame的查询01\n",
    "df.index    #行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行数 列数\n",
    "df.shape "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    int64\n",
       "two    int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列数据类型\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据维度\n",
    "df.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [2, 1],\n",
       "       [3, 3]], dtype=int64)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对象值，二维ndarray数组\n",
    "df.values "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   one  two\n",
       "a    1    2\n",
       "b    2    1\n",
       "c    3    3"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#显示头部几行，默认5行\n",
    "df.head(3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>3</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   one  two\n",
       "a    1    2\n",
       "b    2    1\n",
       "c    3    3"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#显示尾部几行，默认5行\n",
    "df.tail(3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>one</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>two</th>\n      <td>2</td>\n      <td>1</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "     a  b  c\n",
       "one  1  2  3\n",
       "two  2  1  3"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T  #转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 3 entries, a to c\n",
      "Data columns (total 2 columns):\n",
      " #   Column  Non-Null Count  Dtype\n",
      "---  ------  --------------  -----\n",
      " 0   one     3 non-null      int64\n",
      " 1   two     3 non-null      int64\n",
      "dtypes: int64(2)\n",
      "memory usage: 152.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "#相关信息概览：行数，列数，列索引，列非空值个数，列类型，内存占用\n",
    "df.info() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [2, 1],\n",
       "       [3, 3]], dtype=int64)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values  #获取值，ndarray类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>3.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>2.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>1.5</td>\n      <td>1.5</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>2.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2.5</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>3.0</td>\n      <td>3.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "       one  two\n",
       "count  3.0  3.0\n",
       "mean   2.0  2.0\n",
       "std    1.0  1.0\n",
       "min    1.0  1.0\n",
       "25%    1.5  1.5\n",
       "50%    2.0  2.0\n",
       "75%    2.5  2.5\n",
       "max    3.0  3.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()  #统计信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    False\n",
       "b    False\n",
       "c     True\n",
       "Name: one, dtype: bool"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['one']>2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>False</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "     one    two\n",
       "a  False   True\n",
       "b   True  False\n",
       "c  False  False"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否包含某元素:与series相同，可以使用isin()方法，并获取符合条件的元素\n",
    "df.isin([0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>NaN</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>2.0</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   one  two\n",
       "a  NaN  2.0\n",
       "b  2.0  NaN\n",
       "c  NaN  NaN"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.isin([0,2])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index.is_unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>W</th>\n      <th>X</th>\n      <th>Y</th>\n      <th>Z</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   W  X   Y   Z\n",
       "A  0  1   2   3\n",
       "B  4  5   6   7\n",
       "C  8  9  10  11"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = pd.DataFrame(np.arange(12).reshape(3,4),index=list('ABC'),columns=list(('WXYZ')))\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame的切片01: DataFrame有行索引和列索引。DataFrame同样可以通过标签和位置两种方法进行索引和切片。\n",
    "# 方法1：两个中括号，先取列再取行。 比如：df['A'][0]\n",
    "t['W']['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    5\n",
       "C    9\n",
       "Name: X, dtype: int32"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t['X']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>W</th>\n      <th>X</th>\n      <th>Y</th>\n      <th>Z</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>0</td>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>8</td>\n      <td>9</td>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   W  X   Y   Z\n",
       "A  0  1   2   3\n",
       "B  4  5   6   7\n",
       "C  8  9  10  11"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2（推荐）：使用loc/iloc属性，一个中括号，逗号隔开，先取行再取列。loc属性：解释为标签; iloc属性：解释为下标\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.loc['B','Y']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Y    6\n",
       "Z    7\n",
       "Name: B, dtype: int32"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.loc['B',['Y','Z']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Y</th>\n      <th>Z</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "   Y  Z\n",
       "A  2  3\n",
       "B  6  7"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.loc[['A','B'],['Y','Z']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Y</th>\n      <th>Z</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    Y   Z\n",
       "A   2   3\n",
       "B   6   7\n",
       "C  10  11"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.loc['A':,['Y','Z']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "Name: one, dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['a':'c','one']#逗号前面是行，后面是列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Y</th>\n      <th>Z</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>B</th>\n      <td>6</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>10</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": [
       "    Y   Z\n",
       "B   6   7\n",
       "C  10  11"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.iloc 通过位置获取行数据\n",
    "t.iloc[1:3,[2,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "W    0\n",
       "X    1\n",
       "Y    2\n",
       "Z    3\n",
       "Name: A, dtype: int32"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.iloc[0]#默认是第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    5\n",
       "C    9\n",
       "Name: X, dtype: int32"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.iloc[:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# df排序： df.sort_values(by=\"Count_AnimalName\",ascending=False)\n",
    "# 判断数据是否为NaN：pd.isnull(df),pd.notnull(df)缺失数据的处理\n",
    "# NaN的数据:\n",
    "# 处理方式1：删除NaN所在的行列dropna (axis=0, how='any', inplace=False)需要填写axis制定是行删除还是列删除\n",
    "# 处理方式2：填充数据，t.fillna(t.mean()),t.fiallna(t.median()),t.fillna(0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit ('base': conda)",
   "name": "python385jvsc74a57bd0400673a8ac0ec28aa8cb57314b4cf61dae73602632d5d13300e749f49a50fa05"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}